import 'package:library_base/entities/stock_out_order_detail_child_entity.dart';
import 'package:library_base/entities/stock_out_order_detail_entity.dart';
import 'package:library_base/entities/stock_out_order_goods_detail_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:get/get.dart';

class OrderDetailController extends BaseListController<StockOutOrderDetailEntity, StockOutOrderDetailChildEntity> {

  late String id;

  late String stockOutId;

  double finishedCount = 0.0;

  double totalCount = 0.0;

  bool enable = false;

  late Function() callback;

  @override
  void onCreate() {
    enableLoadMore = false;
    isShowNoMoreFooter = false;
    id = getArguments()['id'];
    callback = getArguments()['callback'];
  }

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
    return super.requestData();
  }

  @override
  List<StockOutOrderDetailChildEntity> transformObject(StockOutOrderDetailEntity data, bool isRefresh) {
    stockOutId  = data.id;
    totalCount = 0.0;
    finishedCount = 0.0;
    for(int i = 0;i<data.details.length;i++){
      StockOutOrderDetailChildEntity detail = data.details[i];
      totalCount = NumUtil.add(detail.planOutCount, totalCount);
      for(int i = 0;i<detail.goodsDetails.length;i++){
        StockOutOrderGoodsDetailEntity goodsDetail = detail.goodsDetails[i];
        finishedCount = NumUtil.add(goodsDetail.stockOutCount??0.0, finishedCount);
      }
    }
    return data.details;
  }

  @override
  void loadAfter() {
    enable = isCanDeliverGoods();
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
  }

  void reset() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
  }

  void filter(String time) {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.STOCK_OUT_DETAIL,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "id": id,
        });
  }

  ///预分配重量
  void preDistribution(StockOutOrderDetailChildEntity taskEntity, int position,bool enablePreDistributedGoods){
    if(taskEntity.goodsDetails.isEmpty){
      ToastUtil.normal('暂无库存商品可分配');
      return;
    }
    var temporaryData = StockOutOrderDetailChildEntity.fromJson(taskEntity.toJson());
    if(taskEntity.singleEntry){
      var map = {};
      map['title'] = MultiLanguage.select.tr;
      map['goodsDetails'] = taskEntity.goodsDetails;
      map['planOutCount'] = taskEntity.planOutCount;
      map['singleEntry'] = taskEntity.singleEntry;
      map['goodsName'] = taskEntity.goodsName;
      map['stockOutId'] = stockOutId;
      map['goodsCode'] = taskEntity.goodsCode;
      map['index'] = position;
      map['temporaryData'] = temporaryData;
      map['unit'] = taskEntity.unit;
      map['enablePreDistributedGoods'] = enablePreDistributedGoods;
      map['callback'] = updateUi;
      Get.toNamed(Routers.INVENTORY_DISTRIBUTION_SINGLE_PAGE,arguments: map);
    }else{
      var map = {};
      var temporaryData = StockOutOrderDetailChildEntity.fromJson(taskEntity.toJson());
      map['title'] = MultiLanguage.select.tr;
      map['goodsDetails'] = taskEntity.goodsDetails;
      map['singleEntry'] = taskEntity.singleEntry;
      map['goodsName'] = taskEntity.goodsName;
      map['stockOutId'] = stockOutId;
      map['goodsCode'] = taskEntity.goodsCode;
      map['planOutCount'] = taskEntity.planOutCount;
      map['index'] = position;
      map['temporaryData'] = temporaryData;
      map['unit'] = taskEntity.unit;
      map['enablePreDistributedGoods'] = enablePreDistributedGoods;
      map['callback'] = updateUi;
      Get.toNamed(Routers.INVENTORY_DISTRIBUTION_NO_SINGLE_PAGE,arguments: map);
    }
  }

  void updateUi(StockOutOrderDetailChildEntity stockOutOrderDetailChildEntity, int position){
    tryReLoad();
    // listData[position] = stockOutOrderDetailChildEntity;
    // totalCount = 0.0;
    // finishedCount = 0.0;
    // for(int i = 0;i<listData.length;i++){
    //   StockOutOrderDetailChildEntity detail = listData[i];
    //   totalCount = NumUtil.add(detail.planOutCount, totalCount);
    //   for(int i = 0;i<detail.goodsDetails.length;i++){
    //     StockOutOrderGoodsDetailEntity goodsDetail = detail.goodsDetails[i];
    //     finishedCount = NumUtil.add(goodsDetail.stockOutCount??0.0, finishedCount);
    //   }
    // }
    // enable = isCanDeliverGoods();
    // baseState!.setState(() {});
  }

  ///发货
  void deliverGoods(){
    requestUniversal<bool>(Apis.FINISH_RECORDS,
        tipSuccess: MultiLanguage.successfulDelivery.tr,
        isShowToastTip: true,
        params: {"stockOutId":stockOutId}, resultSuccessCall: (result) async {
          callback();
          GetHelper.back();
        });
  }

  ///打印
  void print(){
  }

  bool isCanDeliverGoods(){
    for(int i = 0;i<listData.length;i++){
      StockOutOrderDetailChildEntity detail = listData[i];
      if(detail.actualOutWeight<=0.0){
        return false;
      }
    }
    return true;
  }

}
